using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace DailyTransaction
{
    public partial class frmBatchUpdate : Form
    {
        private BAO bao = new BAO();
        private string uCMSCenter;
        private string uDateFrom, uDateto;
        private bool uCurr_mth_table;
        
        //private ProgressBar pbar = new ProgressBar();
        public frmBatchUpdate()
        {
            InitializeComponent();            
//            toolStripProgressBar1.ProgressBar = pbar;
            
        }

        private void frmBatchUpdate_Load(object sender, EventArgs e)
        {
            //System.Diagnostics.Process.Start("exec master..xp_cmdshell 'dtsrun /S (local) /U /P /E /NDTS_current_data'");

            cbo_CMSCenter.Focus();
            dtpFrom.Value = DateTime.Now.Date.AddDays(-1);
            dtpTo.Value = DateTime.Now.Date.AddDays(-1);

            lCheckCurrRevenue.DataSource = bao.get_qCheck_curr_revenue();

            cbo_CMSCenter.DataSource = bao.get_qCMS_Center();
            cbo_CMSCenter.DisplayMember = "CMS_Center_Desc";
            cbo_CMSCenter.ValueMember = "CMS_Center";
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            if (cbo_CMSCenter.SelectedValue == null)
            {
                MessageBox.Show("Please select the appropriate CMS Center code !!");
                cbo_CMSCenter.Focus();
                return;
            }
            else
                uCMSCenter = cbo_CMSCenter.SelectedValue.ToString();

            uDateFrom = dtpFrom.Value.ToShortDateString();
            uDateto = dtpTo.Value.ToShortDateString();

            //check the Data Source type - current month (revenue.dbf) or not current month (revenuey.dbf)
            if (rbtYTD.Checked == true)
                uCurr_mth_table = false;
            else if (rbtCurr.Checked == true)
                uCurr_mth_table = true;


            // Start processing
            // Step 1 - Deleting the old records in "temp_Revenue" table
            scr_StatusBar.Text = "Step 1 - Deleting temp_Revenue table";            
            bao.Delete_temp_Revenue();            
            
            // Step 2 - Deleting the old records in "temp_Proc_Phar" table
            scr_StatusBar.Text = "Step 2 - Deleting temp_Proc_Phar table";                        
            bao.Delete_temp_Proc_Phar();

            // Step 3 - Duplicating records from "Act_Revenue into temp_Revenue" table
            scr_StatusBar.Text = "Step 3 - Duplicating records from Act_Revenue into temp_Revenue table";
                        
            int tt = 0;
            if (uCurr_mth_table == true)
            {
                bao.Duplicate_curr_Revenue_Direct(uDateFrom, uDateto, uCMSCenter);
                bao.Duplicate_curr_Revenue(uDateFrom, uDateto, uCMSCenter);
            }
            else
            {
                tt = bao.Duplicate_Revenuey_Direct(uDateFrom, uDateto, uCMSCenter);
                tt = bao.Duplicate_Revenuev(uDateFrom, uDateto, uCMSCenter);
                tt = bao.Duplicate_Revenuey(uDateFrom, uDateto, uCMSCenter);
            }

            // Step 4 - Duplicating records from "Act_Proy into temp_Proc_Phar" table
            scr_StatusBar.Text = "Step 4 - Duplicating records from Act_Proy into temp_Proc_Phar table";
            
            if (uCurr_mth_table == true)
                bao.Duplicate_Curr_Act_proc(uDateFrom, uDateto, uCMSCenter);
            else
            {
                bao.Duplicate_Act_prov(uDateFrom, uDateto, uCMSCenter);
                bao.Duplicate_Act_proy(uDateFrom, uDateto, uCMSCenter);
            }

            // Step 5 - Duplicating records from "Act_Phay into temp_Proc_Phar" table
            scr_StatusBar.Text = "Step 5 - Duplicating records from Act_Phay into temp_Proc_Phar table";
            
            if (uCurr_mth_table == true)
                bao.Duplicate_curr_Act_phar(uDateFrom, uDateto, uCMSCenter);
            else
            {                
                bao.Duplicate_Act_phav(uDateFrom, uDateto, uCMSCenter);
                bao.Duplicate_Act_phay(uDateFrom, uDateto, uCMSCenter);
            }

            //Step 6 - Update rate for temp_revenue table
            // Modified on 30/07/2009
            // added CMS_Ceneter parameter
            // commented on 31/7/2009
            //bao.Update_totals_temp_revenue(cbo_CMSCenter.SelectedValue.ToString());
            //bao.Update_pays_temp_proc_phar(cbo_CMSCenter.SelectedValue.ToString());
            //end
            //bao.Update_Rounding_Totals();
            //bao.Update_Rounding_Pays_Temp_proc_phar();

            //Step 6 - Batch update of the temp_revenue table..
            scr_StatusBar.Text = "Step 6 - Batch Update for the Invoice Type info.";
            bao.Batch_update_Invoice_type();            
           // bao.Batch_update_Invoice_type();

            // Step 7 - Batch update of the temp_proc_phar table..
            scr_StatusBar.Text = "Step 7 - Batch Update for the Doctor's Code info.";                        
            //bao.Batch_Update_Doc_code();
            bao.Batch_update_Doc_code();          

            // Step 8 - Append the Batch updated temp_revenue into the new_revenue table
            //scr_StatusBar.Text = "Step 8 - Append the batch updated temp_revenue records into new_revenue table";            
            //Modified on 31/7/2009
            //Step 8 - Append the Batch updated temp_revenue into the new_revenue_VN table            
            scr_StatusBar.Text = "Step 8 - Append the batch updated temp_revenue records into new_revenue_VN table";
            bao.Append_Rev2Table();

            // Step 9 - Append the Batch updated temp_proc_phar records into the new_proc_phar table
            //scr_StatusBar.Text = "Step 9 - Append the Batch updated temp_proc_phar records into the new_proc_phar table ";                        
            //Modified on 31/7/2009
            //Step 9 - Append the Batch updated temp_proc_phar records into the new_proc_phar_VN table
            scr_StatusBar.Text = "Step 9 - Append the Batch updated temp_proc_phar records into the new_proc_phar_VN table ";            
            bao.Append_Proc_Phar2Table();

            // Buu Update
            bao.qAppend_VAT_CMS_NUM(uDateFrom, uDateto);
            bao.qUpdate_PayAccountCode_AC_GTE(uDateFrom, uDateto);
            bao.qUpdate_PayAccountCode_OUTSTD(uDateFrom, uDateto);
            bao.qUpdate_CCP(uDateFrom, uDateto);

            // Buu Update table tTemp_VAT_VOID
            bao.qDelete_AV_INV_To_Temp_VAT_Void();
            bao.qAppend_AV_INV_To_Temp_VAT_Void(uDateFrom, uDateto);
            if (uCurr_mth_table == false)
                bao.Update_ACC_Void_Inv(uDateFrom, uDateto);



            //Added on 31/7/2009
            //Insert data from new_revenue_vn to new_revenue
            bao.Append_RevVN2Rev(uDateFrom, uDateto);
            //Insert data from new_proc_phar_VN to new_proc_phar
            bao.Append_Proc_PharVN2Proc_Phar(uDateFrom, uDateto);
            //Update total to USD
            //bao.Update_totals_temp_revenue(cbo_CMSCenter.SelectedValue.ToString());
            //bao.Update_pays_temp_proc_phar(cbo_CMSCenter.SelectedValue.ToString());
            //end
            bao.usp_InsertIntoDailyTransTemp(dtpFrom.Value.ToShortDateString(), dtpTo.Value.ToShortDateString());
            //MessageBox.Show("Starting to insert data to DailyTrans");
            bao.usp_InsertIntoDailyTrans(dtpFrom.Value.ToShortDateString(), dtpTo.Value.ToShortDateString());
            //MessageBox.Show("Done!");
            //Added on 30/7/2009
           // bao.check_cms_sequence(uDateFrom,uDateto);
            //end 

            scr_StatusBar.Text = "Batch run COMPLETED !! ";            
        }
    }
}